package com.ya;

import io.minio.MinioClient;
import io.minio.Result;
import io.minio.errors.MinioException;
import io.minio.ListObjectsArgs;
import io.minio.messages.Item;

import java.io.FileOutputStream;
import java.io.InputStream;

public class MinioDownloadAll {
    public static void main(String[] args) {
        try {
            // 默认参数
            String endpoint = "http://localhost:9000";
            String accessKey = "minioadmin";
            String secretKey = "minioadmin";
            String bucketName = "micromap";
            String localDirectoryPath = "/Users/wangzhenyang/Downloads/micromap/";

            // 解析命令行参数
            for (int i = 0; i < args.length; i++) {
                switch (args[i]) {
                    case "--e":
                    case "--endpoint":
                        endpoint = args[i + 1];
                        break;
                    case "--a":
                    case "--accessKey":
                        accessKey = args[i + 1];
                        break;
                    case "--s":
                    case "--secretKey":
                        secretKey = args[i + 1];
                        break;
                    case "--b":
                    case "--bucketName":
                        bucketName = args[i + 1];
                        break;
                    case "--l":
                    case "--localDirectoryPath":
                        localDirectoryPath = args[i + 1];
                        break;
                    case "--h":
                    case "--help":
                        System.out.println("Usage: java -jar minio-download-all.jar [--e|--endpoint <endpoint>] " +
                                "[--a|--accessKey <accessKey>] [--s|--secretKey <secretKey>] [--b|--bucketName " +
                                "<bucketName>] [--l|--localDirectoryPath <localDirectoryPath>]");
                        System.out.println("Example: java -jar ss.jar --e http://localhost:9000 --a minioadmin --s " +
                                "minioadmin --b micromap --l /Users/wangzhenyang/Downloads/micromap/\n");
                        break;
                }
            }

            // 初始化 MinIO 客户端
            MinioClient minioClient = MinioClient.builder()
                                                 .endpoint(endpoint)
                                                 .credentials(accessKey, secretKey)
                                                 .build();

            // 获取存储桶中的所有对象
            Iterable<Result<Item>> results = minioClient.listObjects(
                    ListObjectsArgs.builder()
                                   .bucket(bucketName)
                                   .recursive(true)
                                   .build());

            // 遍历对象列表并下载每个对象
            for (Result<Item> result : results) {
                Item item = result.get();
                String objectName = item.objectName();

                // 构建本地文件路径
                String localFilePath = localDirectoryPath + objectName;

                // 获取对象的输入流
                try (InputStream stream = minioClient.getObject(
                        io.minio.GetObjectArgs.builder()
                                              .bucket(bucketName)
                                              .object(objectName)
                                              .build())) {

                    // 创建本地文件输出流
                    FileOutputStream fos = new FileOutputStream(localFilePath);

                    // 缓冲区
                    byte[] buf = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = stream.read(buf)) != -1) {
                        fos.write(buf, 0, bytesRead);
                    }

                    // 关闭输出流
                    fos.close();
                    System.out.println("Object " + objectName + " has been downloaded to " + localFilePath + ".");
                }
            }
        } catch (MinioException e) {
            System.out.println("Error occurred: " + e);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
